package com.mall.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.dao.mapper.AddressMapper;
import com.mall.model.Address;
import com.mall.model.Order;
import com.mall.model.Staff;
import com.mall.service.StaffService;
import com.mall.util.MyUtil;

@Controller
@RequestMapping("/StaffController")
public class StaffController extends MyUtil {
	@Autowired
	private StaffService staffService;
	@Autowired
	private AddressMapper addressMapper;

	/**
	 * 获取员工集合
	 *
	 * @dagewang 2017年8月19日
	 */
	@RequestMapping("/listStaff")
	public @ResponseBody Map<String, Object> listStaff() throws Exception {
		List<Staff> list = staffService.listStaff();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("staff", list);
		return map;
	}

	/**
	 * 获取单个员工
	 *
	 * @dagewang 2017年9月25日
	 */
	@RequestMapping("/get")
	public @ResponseBody Map<String, Object> get(Integer sid) throws Exception {
		Staff staff = staffService.get(sid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("staff", staff);
		return map;
	}

	/**
	 * 管理员登录
	 *
	 * @dagewang 2017年9月25日
	 */
	@RequestMapping("/login")
	public String login(String username, String password, Model model, HttpSession session) {
		if (!this.isNull(username, password)) {
			Staff au = staffService.getByName(username, password);
			if (au != null) {
				au.setPassword(null);
				session.setAttribute("staff", au);
				return "redirect:staffOrder.action";
			}
		}
		model.addAttribute("error", "用户名或密码错误!");
		return "staffLogin";

	}

	/**
	 * 运送订单页
	 *
	 * @dagewang 2017年10月24日
	 */
	@RequestMapping("/staffOrder")
	public String staffOrder(Model model, HttpSession session) {
		Staff attribute = (Staff) session.getAttribute("staff");
		if (attribute == null) {
			return "staffLogin";
		}
		List<Order> order = staffService.getOrders(attribute.getId());
		if (order != null) {
			for (int i = 0; i < order.size(); i++) {
				Order o = order.get(i);
				Address selectByPrimaryKey = addressMapper.selectByPrimaryKey(o.getAddressid());
				o.setImg(selectByPrimaryKey.getAddress());
				o.setClassname(selectByPrimaryKey.getName());
				o.setReason(selectByPrimaryKey.getPhonenum());
			}

		}
		model.addAttribute("orders", order);
		return "staffOrder";
	}

	/**
	 * 修改密码
	 *
	 * @dagewang 2017年10月24日
	 */
	@RequestMapping("/updatepwd")
	public @ResponseBody Map<String, Object> updatepwd(String opwd, String npwd, HttpSession session) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(this.isNull(opwd,npwd)){
			map.put("r", 1);
			return map;
		}
		Staff attribute = (Staff) session.getAttribute("staff");
		if (attribute == null) {
			map.put("r", 1);
			return map;
		}
		Staff staff = staffService.get(attribute.getId());
		if (staff.getPassword().equals(opwd)) {
			staff.setPassword(npwd);
			int result = staffService.update(staff);
			if (result == 1) {
				map.put("r", 0);
				return map;
			}
		}
		map.put("r", 2);
		return map;
	}
	/**
	 * 修改个人资料
	 *
	 *@dagewang 2017年10月24日
	 */
	@RequestMapping("/update")
	public @ResponseBody Map<String, Object> update(String name, String phonenum, HttpSession session) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Staff staff = (Staff) session.getAttribute("staff");
		if(this.isNull(name,phonenum)){
			map.put("r", 2);
		}else if(staff == null){
			map.put("r", 0);
		}else{
			staff.setName(name);
			staff.setPhonenum(phonenum);
			int update = staffService.update(staff);
			map.put("r", update);
		}
			
		return map;
	}
	/**
	 * 退出登录
	 *
	 *@dagewang 2017年10月24日
	 */
	@RequestMapping("/outLogin")
	public String outLogin(HttpSession session){
		session.setAttribute("staff", null);
		return "staffLogin";
	}

	public StaffService getStaffService() {
		return staffService;
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

}
